package com.inlycat.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.inlycat.entity.Thumb;
import com.inlycat.entity.Tramp;
import com.inlycat.entity.VideoAttr;
import com.inlycat.mapper.ThumbMapper;
import com.inlycat.mapper.TrampMapper;
import com.inlycat.mapper.VideoAttrMapper;
import com.inlycat.service.ThumbService;

/**
 * @author jarvan4dev@163.com
 * @since 2015年8月24日下午2:12:32
 * @category
 **/

@Service("thumbService")
public class ThumbServiceImpl implements ThumbService {

	@Resource
	private ThumbMapper thumbMapper;
	
	@Resource
	private TrampMapper trampMapper;

	@Resource
	private VideoAttrMapper videoAttrMapper;

	@Override
	public boolean insert(Thumb thumb) {
		boolean flag = false;
		/**
		 * 点赞之前取消踩
		 */
		Tramp tramp = new Tramp(thumb.getUserId(),thumb.getVideoId());
		if(trampMapper.hasTramp(tramp)>0){
			flag = trampMapper.delete(tramp);
		}
		boolean result = thumbMapper.insert(thumb);
		if (result) {
			VideoAttr videoAttr = new VideoAttr();
			videoAttr.setVideoId(thumb.getVideoId());
			if(flag)
				videoAttr.setTramp(-1);
			videoAttr.setThumb(1);
			result = videoAttrMapper.updateSelectiveByVideoId(videoAttr);
		}
		return result;
	}

	@Override
	public boolean delete(Thumb thumb) {
		boolean result = thumbMapper.delete(thumb);
		if (result) {
			VideoAttr videoAttr = new VideoAttr();
			videoAttr.setVideoId(thumb.getVideoId());
			videoAttr.setThumb(-1);
			result = videoAttrMapper.updateSelectiveByVideoId(videoAttr);
		}
		return result;
	}

	@Override
	public boolean hasThumb(Thumb thumb) {
		boolean result = thumbMapper.hasThumb(thumb) > 0 ? true : false;
		return result;
	}

}
